#include "PowerFlowCalculation.h"
#include "readcsv.h"
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <sstream>
using namespace std;

double string2double(string &s){
    istringstream iss(s);
    double dst;
    iss>>dst;
    return dst;
}

CSVdata read_from_csv_double(char *filename){
    CSVdata cd;
    ifstream csv_file(filename);
    if(csv_file.is_open()){
        //printf("open file suc:%s\n",filename);
        string line_buff;
        getline(csv_file,line_buff);//read the header,drop it
        cd.row=0;
        while(getline(csv_file,line_buff)){
            string e("");
            CSVline *l=new CSVline;
            l->col=0;
            for(int i=0;i<line_buff.length();i++){
                char c=line_buff.at(i);
                if(c==','){
                    l->ele.push_back(string2double(e));
                    e.clear();
                    l->col++;
                }else{
                    e.append(1,c);
                }
            }
            l->col++;
            l->ele.push_back(string2double(e));
            cd.lines.push_back(*l);
            cd.col=l->col;
            cd.row++;
        }

        csv_file.close();
    }else{
        printf("open file error:%s\n",filename);
    }
    return cd;


}



